﻿using System.ComponentModel;
using VisualWip.Core;
using VisualWip.Core.Columns;
using VisualWip.Core.Interfaces;

namespace VisualWip.TFS2010Providers
{
    public abstract class TfsWorkDualQuerySettingsViewModelBase<TColumn> :
        TfsWorkQuerySettingsViewModelBase<TColumn> where TColumn : WorkDualQueryColumnBase
    {
        protected TfsWorkDualQuerySettingsViewModelBase(Application application, TColumn column,
                                                            IWorkItemProviderFactory workItemProviderFactory, IWorkItemSizeProvider workItemSizeProvider,
                                                    IColumnTypeProvider columnTypeProvider)
            : base(application, column, workItemProviderFactory, workItemSizeProvider, columnTypeProvider)
        {
        }

        public QueryFolderViewModel FirstQueries { get; protected set; }
        public QueryDefinitionViewModel FirstSelectedQuery { get; set; }
        public QueryFolderViewModel SecondQueries { get; protected set; }
        public QueryDefinitionViewModel SecondSelectedQuery { get; set; }

        protected override void Init()
        {
            base.Init();
            
            if (WorkItemWorkProvider.IsConnected)
            {
                
                FirstQueries = new QueryFolderViewModel(WorkItemWorkProvider.TeamProject.QueryHierarchy, null);
                SecondQueries = new QueryFolderViewModel(WorkItemWorkProvider.TeamProject.QueryHierarchy, null);

                var firstQuery = Column.FirstQuery as WorkItemQueryBase;
                var selectedQuery = Column.SecondQuery as WorkItemQueryBase;

                Attache(FirstQueries.Children, firstQuery,
                        FirstQueryItemPropertyChanged);
                Attache(SecondQueries.Children, selectedQuery,
                        SecondQueryItemPropertyChanged);

                if (firstQuery != null && firstQuery.DownloadDisplayFieldsOnly)
                    DownloadDisplayFieldsOnly = true;
                
                if (selectedQuery != null && selectedQuery.DownloadDisplayFieldsOnly)
                    DownloadDisplayFieldsOnly = true;
            }
        }

        private void FirstQueryItemPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            FirstSelectedQuery = sender as QueryDefinitionViewModel;

            if (FirstSelectedQuery == null)
                return;

            SetColumnName(FirstSelectedQuery);


            Column.FirstQuery = CreateWorkItemQuery(FirstSelectedQuery);
        }

        private void SecondQueryItemPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            SecondSelectedQuery = sender as QueryDefinitionViewModel;

            if (SecondSelectedQuery == null)
                return;

            SetColumnName(SecondSelectedQuery);

            Column.SecondQuery = CreateWorkItemQuery(SecondSelectedQuery);
        }
    }
}